package com.cndw.kungfu.event.model;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

import com.cndw.kungfu.domain.Notice;
import com.cndw.kungfu.event.BaseHandler;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.sys.ChatMain;

public class BusinessNoticeHandler extends BaseHandler implements Runnable {

	private Notice notice;
	private String name;
	private ScheduledFuture<?> task;
	private static List<BusinessNoticeHandler> handlers = new ArrayList<BusinessNoticeHandler>();
	private static volatile int numSign = 0;

	public BusinessNoticeHandler(Notice e) {
		notice = e;
		numSign++;
		name = "notice " + numSign;
		// LogUtil.getMain().debug("start "+e.getTitle());
	}

	@Override
	public void run() {
		// LogUtil.getMain().debug(1);
		if (notice.getEndTime() > 0 && notice.getEndTime() < TimeProcesser.getUnixTime()) {
			stop();
			handlelDel(this);
			return;
		}
		if (notice.getBeginTime() > 0 && notice.getBeginTime() > TimeProcesser.getUnixTime()) {
			return;
		}
		// LogUtil.getMain().debug(notice.getTitle());
		ChatMain.systemMessage(notice.getTitle());
		// log.info("====");
	}

	private void stop() {

		if (task != null) {
			task.cancel(false);
		} else {
			LogUtil.getMain().info("stop err " + name);
		}

	}

	public void setTask(ScheduledFuture<?> task) {
		this.task = task;
	}

	private static synchronized void handlerAdd(BusinessNoticeHandler e) {
		handlers.add(e);
	}

	private static synchronized void handlelDel(BusinessNoticeHandler e) {
		handlers.remove(e);
	}

	public static synchronized void runBoss() {
		List<Notice> notices = Context.getCongfigServer().getBusinessNoticesDB();

		int currentTime = TimeProcesser.getUnixTime();
		if (notices != null) {
			LogUtil.getMain().info("notice init in  size " + notices.size());
			int initDelay = 0;
			for (Notice e : notices) {
				if (e.getEndTime() > 0 && e.getEndTime() <= currentTime) {
					continue;// 已结束的不启动
				}
				initDelay += 3;

				BusinessNoticeHandler command = new BusinessNoticeHandler(e);
				ScheduledFuture<?> task = Context.getTimerServer().scheduleAtFixedRate(command, initDelay, e.getInitialDelay());
				command.setTask(task);
				handlerAdd(command);

			}
		}
	}

	public static synchronized void restart() {
		try {
			for (BusinessNoticeHandler e : handlers) {
				if (e != null) {
					try {
						e.stop();
					} catch (Exception e2) {
						e2.printStackTrace();
					}

				}
			}
			handlers.clear();
			LogUtil.getMain().info("notice restart ....");
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			List<Notice> notices = Context.getCongfigServer().getBusinessNoticesDB();
			if (notices != null) {
				LogUtil.getMain().info("notice size " + notices.size());
			}
			runBoss();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
